﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using NGE;

namespace MH.SE
{
    public partial class NPCInfo : UserControl
    {
        public NPCInfo()
        {
            InitializeComponent();

        }

        Game game;

        public void LoadScene(Game g)
        {
            game = g;
            refresh2();
        }

        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }
        Character npc;

        public void refresh()
        {
            if (npc == null)
            {
                return;
            }
            textBox26.Text = "";
            textBox1.Text = npc.Name;
            richTextBox1.Text = npc.Scrpit;
            textBox2.Text = npc.DefaultLocation.X / sceneCOM.SCN.TileSize.Width + "";
            textBox3.Text = npc.DefaultLocation.Y / sceneCOM.SCN.TileSize.Height + "";
            comboBox1.Text = npc.DefaultDirection.ToString();
            textBox4.Text = npc.Avatar.GUID + "";
            textBox6.Text = npc.AvatarSize.Width / sceneCOM.SCN.TileSize.Width + "";
            textBox5.Text = npc.AvatarSize.Height / sceneCOM.SCN.TileSize.Height + "";
            checkBox1.Checked = npc.IsEnemy;
            checkBox2.Checked = npc.IsAlwaysMoving;
            textBox7.Text = npc.MoveSpeed.ToString();


            textBox15.Text = npc.MonsterDamage + "";
            textBox18.Text = npc.MonsterDefense + "";
            textBox17.Text = npc.MonsterAccurate + "";
            textBox16.Text = npc.MonsterMissrate + "";
            textBox19.Text = npc.MonsterArmor + "";

            textBox21.Text = npc.MonsterAttackRangeMin + "";
            textBox22.Text = npc.MonsterAttackRangeMax + "";
            textBox23.Text = npc.MonsterMoveRangeMax + "";


            textBox8.Text = npc.HP + "";
            if (npc.Type == SpriteType.NPC)
            {
                radioButton1.Checked = true;
            }
            if (npc.Type == SpriteType.Box)
            {
                radioButton2.Checked = true;
            }
            if (npc.Type == SpriteType.Door)
            {
                radioButton3.Checked = true;
            }
            if (npc.Type == SpriteType.Monster)
            {
                radioButton4.Checked = true;
            }

            
            if (npc.Spoils != null)
            {
                foreach (Spoil s in npc.Spoils)
                {
                    textBox26.Text += s.Name + "=" + s.Seed + "\r\n";
                }
            }
        }
        public void refresh2()
        {
            if (sceneCOM.SCN.NPCs == null)
            {
                return;
            }
            comboBox2.Items.Clear();
            foreach (Character n in sceneCOM.SCN.NPCs)
            {
                comboBox2.Items.Add(n.Name);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = comboBox2.SelectedIndex;
            if (i > -1)
            {
                npc = sceneCOM.SCN.NPCs[i];
            }
            refresh();
        }

        Character setnpc(Character npc)
        {
            npc.IsAlwaysMoving = checkBox2.Checked;
            npc.MoveSpeed = float.Parse(textBox7.Text);

            if (radioButton1.Checked)
            {
                npc.Type = SpriteType.NPC;
            }
            else if (radioButton2.Checked)
            {
                npc.Type = SpriteType.Box;
                npc.BoxState = BoxState.Closed;
            }
            else if (radioButton3.Checked)
            {
                npc.Type = SpriteType.Door;
                npc.DoorState = DoorState.Closed;
            }
            else if (radioButton4.Checked)
            {
                npc.Type = SpriteType.Monster;
            }

            npc.Name = textBox1.Text;
            npc.Scrpit = richTextBox1.Text;
            npc.IsEnemy = checkBox1.Checked;

            int x = int.Parse(textBox2.Text);
            x *= sceneCOM.SCN.TileSize.Width;
            int y = int.Parse(textBox3.Text);
            y *= sceneCOM.SCN.TileSize.Height;

            npc.Location = npc.DefaultLocation = new Microsoft.DirectX.Vector3(x, y, 0);
            npc.Direction = npc.DefaultDirection = (Direction)Enum.Parse(typeof(Direction), comboBox1.Text);
            try
            {
                npc.Avatar.GetData(textBox4.Text);
            }
            catch { }

            int w = int.Parse(textBox6.Text);
            w *= sceneCOM.SCN.TileSize.Width;
            int h = int.Parse(textBox5.Text);
            h *= sceneCOM.SCN.TileSize.Height;
            npc.AvatarSize = new System.Drawing.Size(w, h);


            try
            {
                npc.HP = int.Parse(textBox8.Text);
                npc.MonsterDamage = int.Parse(textBox15.Text);
                npc.MonsterDefense = int.Parse(textBox18.Text);
                npc.MonsterAccurate = int.Parse(textBox17.Text);
                npc.MonsterMissrate = int.Parse(textBox16.Text);
                npc.MonsterArmor = int.Parse(textBox19.Text);

                npc.MonsterAttackRangeMax = int.Parse(textBox22.Text);
                npc.MonsterAttackRangeMin = int.Parse(textBox21.Text);
                npc.MonsterMoveRangeMax = int.Parse(textBox23.Text);
            }
            catch { }

            if (npc.Spoils == null)
            {
                npc.Spoils = new List<Spoil>();
            }
            npc.Spoils.Clear();
            string tmp = textBox26.Text;
            string[] tmp2 = tmp.Split(new string[] { "=", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < tmp2.Length; i += 2) 
            {
                Spoil spoil = new Spoil();
                spoil.Name = tmp2[i];
                spoil.Seed = int.Parse(tmp2[i + 1]);
                npc.Spoils.Add(spoil);
            }

            return npc;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int i = comboBox2.SelectedIndex;
            if (i > -1)
            {
                sceneCOM.SCN.NPCs.RemoveAt(i);
                comboBox2.Text = "";
            }
            refresh2();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (sceneCOM.SCN.NPCs == null)
            {
                sceneCOM.SCN.NPCs = new List<Character>();
            }
            Character chr = setnpc(new Character());
            sceneCOM.SCN.NPCs.Add(chr);
            sceneCOM.CHRs.Add(chr);
            refresh2();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int i = comboBox2.SelectedIndex;
            if (i == -1)
            {
                return;
            }
            setnpc(sceneCOM.SCN.NPCs[i]);
            refresh2();
        }
        NImage tmpIMG;

        private void textBox4_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "*.png|*.png";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = fd.FileName;
                tmpIMG = new NImage();
                tmpIMG.GetData(textBox4.Text);
            }
        }

        private void richTextBox1_Click(object sender, EventArgs e)
        {
            int i = comboBox2.SelectedIndex;
            if (i == -1)
            {
                return;
            }
            CodeEditor ce = new CodeEditor();
            ce.CODE = sceneCOM.SCN.NPCs[i].Scrpit;
            if (ce.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Text = ce.CODE;
                sceneCOM.SCN.NPCs[i].Scrpit = ce.CODE;
            }
        }
    }
}
